﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using com.subb.WebInformation;
using WebEntities;
using WebEntities.Models.Framework;
using Tools.com.Pager;
using com.subb.ProductInformation;
using com.subb.WebFramework;
using WebsiteUI.Extensions;
using WebEntities.Models.WebInfo;

namespace WebsiteUI.Controllers
{
    public class ListController : Controller
    {
        private const int PageRows = 30;
        /// <summary>
        /// 文字列表
        /// </summary>
        /// <param name="id">新闻目录ID</param>
        /// <returns></returns>
        [OutputCache(CacheProfile = "Cache15Mins", VaryByParam = "*")]
        public ActionResult Index(int id = 100, int page = 1)
        {
            page = page < 1 ? 1 : page;
            int start = (page - 1) * PageRows;
            int ttlCount = 0;
            int cataId = id;
            string cataName = string.Empty;
            List<WI_News> newsList = new List<WI_News>();
            List<WI_News> leftNewsList = new List<WI_News>();  //左侧新闻栏列表
            List<WI_NewsCata> cataList = new List<WI_NewsCata>();
            WI_NewsCataDAL ncdal = new WI_NewsCataDAL();
            WI_NewsDAL ndal = new WI_NewsDAL();
            WI_NewsCata ncata = ncdal.GetNewsCata(id);
            if (ncata != null)
            {
                if (ncata.ParentCataID.HasValue)
                {
                    cataId = ncata.ParentCataID ?? 0;
                    cataList = ncdal.GetAllNewsCataByPcataId(ncata.ParentCataID, 0, 20);
                    newsList = ndal.GetAllNews(id, start, PageRows, out ttlCount, string.Empty);  //二级新闻列表
                    leftNewsList = ndal.GetAllNewsByPCataID(cataId, 0, 8);
                    WI_NewsCata cata1 = ncdal.GetNewsCata(cataId);
                    cataName = cata1.CataName;
                }
                else
                {
                    cataList = ncdal.GetAllNewsCataByPcataId(ncata.CataID, 0, 20);
                    if (cataList.Count > 0)
                    {
                        //有下级新闻目录
                        newsList = ndal.GetAllNewsByPCataID(cataId, start, PageRows, out ttlCount, string.Empty);  //下级新闻列表
                        leftNewsList = ndal.GetAllNewsByPCataID(cataId, 0, 8);
                    }
                    else
                    {
                        //没有下级新闻目录
                        newsList = ndal.GetAllNews(id, start, PageRows, out ttlCount, string.Empty);  //一级新闻列表
                        leftNewsList = ndal.GetAllNews(cataId, 0, 8);
                    }
                    cataName = ncata.CataName;
                }
            }

            ViewBag.ArgID = id; //作为参数传给圆圈局部视图
            ViewBag.CataID = cataId;  //一级新闻目录ID
            ViewBag.CataName = cataName;  //一级新闻目录名称
            ViewBag.NewsCataList = cataList;
            PagedList<WI_News> pglist = newsList.ToPagedList(page, PageRows, ttlCount);

            //获取最新新闻列表
            ViewBag.LeftNewsList = leftNewsList;

            return View(pglist);
        }

        /// <summary>
        /// 图文列表页面
        /// </summary>
        /// <param name="id">新闻目录ID</param>
        [OutputCache(CacheProfile = "Cache15Mins", VaryByParam = "*")]
        public ActionResult view(int id = 100, int page = 1)
        {
            page = page < 1 ? 1 : page;
            int start = (page - 1) * PageRows;
            int ttlCount = 0;
            int cataId = id;
            string cataName = string.Empty;
            List<WI_News> newsList = new List<WI_News>();
            List<WI_News> leftNewsList = new List<WI_News>();  //左侧新闻栏列表
            List<WI_NewsCata> cataList = new List<WI_NewsCata>();
            WI_NewsCataDAL ncdal = new WI_NewsCataDAL();
            WI_NewsDAL ndal = new WI_NewsDAL();
            WI_NewsCata ncata = ncdal.GetNewsCata(id);
            if (ncata != null)
            {
                if (ncata.ParentCataID.HasValue)
                {
                    cataId = ncata.ParentCataID ?? 0;
                    cataList = ncdal.GetAllNewsCataByPcataId(ncata.ParentCataID, 0, 20);
                    newsList = ndal.GetAllNews(id, start, PageRows, out ttlCount, string.Empty);  //二级新闻列表
                    leftNewsList = ndal.GetAllNewsByPCataID(cataId, 0, 8);
                    WI_NewsCata cata1 = ncdal.GetNewsCata(cataId);
                    cataName = cata1.CataName;
                }
                else
                {
                    cataList = ncdal.GetAllNewsCataByPcataId(ncata.CataID, 0, 20);
                    if (cataList.Count > 0)
                    {
                        //有下级新闻目录
                        newsList = ndal.GetAllNewsByPCataID(cataId, start, PageRows, out ttlCount, string.Empty);  //下级新闻列表
                        leftNewsList = ndal.GetAllNewsByPCataID(cataId, 0, 8);
                    }
                    else
                    {
                        //没有下级新闻目录
                        newsList = ndal.GetAllNews(id, start, PageRows, out ttlCount, string.Empty);  //一级新闻列表
                        leftNewsList = ndal.GetAllNews(cataId, 0, 8);
                    }
                    cataName = ncata.CataName;
                }
            }

            ViewBag.ArgID = id; //作为参数传给圆圈局部视图
            ViewBag.CataID = cataId;  //一级新闻目录ID
            ViewBag.CataName = cataName;  //一级新闻目录名称
            ViewBag.NewsCataList = cataList;
            PagedList<WI_News> pglist = newsList.ToPagedList(page, PageRows, ttlCount);

            //获取最新产品列表
            PP_ProductDAL pdal = new PP_ProductDAL();
            int prodTtlCount;
            ViewBag.LeftProdList = pdal.GetAllProduct(0, 8, out prodTtlCount);

            return View(pglist);
        }

        /// <summary>
        /// 专家列表页面
        /// </summary>
        /// id 专家目录ID，默认0为全部
        /// <returns></returns>
        [OutputCache(Duration = 3 * 60 * 60, VaryByParam = "*")]
        public ActionResult Expert(int? page, int id = 0)
        {
            ViewBag.MenuItemID = CommonFn.GetContentID("ExpertMuItemID");
            WI_ExpertCataDAL cataDal = new WI_ExpertCataDAL();
            int cataTotal = 0;
            ViewBag.ExpertCataList = cataDal.GetAllExpertCata(0, 8, out cataTotal);
            int start = ((page ?? 1) - 1) * 12;
            int PageTotal = 0;
            WI_ExpertsDAL DAL = new WI_ExpertsDAL();
            List<WI_Experts> list = DAL.GetAllExpertsByView(id, start, 12, out PageTotal);
            PagedList<WI_Experts> pageList = list.ToPagedList(page ?? 0, 12, PageTotal);
            return View(pageList);
        }

        /// <summary>
        /// 产品列表 页面
        /// </summary>
        /// <param name="page"></param>
        /// <param name="id">产品目录ID，默认0为全部</param>
        /// <returns></returns>
        //[OutputCache(Duration = 3 * 60 * 60, VaryByParam = "*")]
        [OutputCache(CacheProfile = "Cache15Mins", VaryByParam = "*")]
        public ActionResult Product(int? page, int id = 0)
        {
            ViewBag.MenuItemID = CommonFn.GetContentID("ProdMuItemID");
            PP_ProductCataDAL cataDal = new PP_ProductCataDAL();
            ViewBag.ProductCataList = cataDal.GetAllProductCata();
            int start = ((page ?? 1) - 1) * 10;
            int PageTotal = 0;
            PP_ProductDAL DAL = new PP_ProductDAL();
            List<PP_Product> list = DAL.GetAllUiProduct(id, start, 10, out PageTotal);
            PagedList<PP_Product> pageList = list.ToPagedList(page ?? 0, 10, PageTotal);
            return View(pageList);
        }

        public ActionResult SearchNews(string search, string stype, int page = 1)
        {
            string decSearch = HttpUtility.UrlDecode(search.Trim());
            ViewBag.SearchKey = decSearch;
            page = page < 1 ? 1 : page;
            int start = (page - 1) * PageRows;
            int ttlCount;
            WI_NewsDAL ndal = new WI_NewsDAL();
            List<WI_News> newslist = new List<WI_News>();
            if (stype.ToLower() == "newstitle")
            {
                newslist = ndal.SearchNewsByTitle(start, PageRows, out ttlCount, decSearch);
            }
            else
            {
                newslist = ndal.SearchNewsByDescr(start, PageRows, out ttlCount, decSearch);
            }
            PagedList<WI_News> pglist = newslist.ToPagedList(page, PageRows, ttlCount);
            ViewBag.SearchCount = ttlCount;
            return View(pglist);
        }

        [HttpPost]
        public ActionResult SearchNews(string search, string stype)
        {
            string encSearch = HttpUtility.UrlEncode(search.Trim());
            return RedirectToAction("SearchNews", "list", new { search = encSearch, stype = stype });
        }

        public ActionResult SearchProduct(string search, string stype, int page = 1)
        {
            string decSearch = HttpUtility.UrlDecode(search.Trim());
            ViewBag.SearchKey = decSearch;
            page = page < 1 ? 1 : page;
            int start = (page - 1) * PageRows;
            int ttlCount;
            PP_ProductDAL pdal = new PP_ProductDAL();
            List<PP_Product> prodList = new List<PP_Product>();
            if (stype.ToLower() == "prodname")
            {
                prodList = pdal.SearchByName(decSearch, start, PageRows, out ttlCount);
            }
            else
            {
                prodList = pdal.SearchByDescr(decSearch, start, PageRows, out ttlCount);
            }
            PagedList<PP_Product> pglist = prodList.ToPagedList(page, PageRows, ttlCount);
            ViewBag.SearchCount = ttlCount;
            return View(pglist);
        }

        [HttpPost]
        public ActionResult SearchProduct(string search, string stype)
        {
            string encSearch = HttpUtility.UrlEncode(search.Trim());
            return RedirectToAction("SearchProduct", "list", new { search = encSearch, stype = stype });
        }

        public ActionResult SearchExpert(string search, int page = 1)
        {
            string decSearch = HttpUtility.UrlDecode(search.Trim());
            ViewBag.SearchKey = decSearch;
            page = page < 1 ? 1 : page;
            int start = (page - 1) * PageRows;
            int ttlCount;
            WI_ExpertsDAL edal = new WI_ExpertsDAL();
            List<WI_Experts> explist = edal.SearchByName(decSearch, start, PageRows, out ttlCount);
            PagedList<WI_Experts> pglist = explist.ToPagedList(page, PageRows, ttlCount);
            ViewBag.SearchCount = ttlCount;
            return View(pglist);
        }

        [HttpPost]
        public ActionResult SearchExpert(string search)
        {
            string encSearch = HttpUtility.UrlEncode(search.Trim());
            return RedirectToAction("SearchExpert", "list", new { search = encSearch });
        }

        // [OutputCache(CacheProfile = "Cache15Mins")]
        public ActionResult Jobs(int page = 1)
        {
            ViewBag.MenuItemID = CommonFn.GetContentID("JobsMuItemID");
            page = page < 1 ? 1 : page;
            int start = (page - 1) * PageRows;
            int ttlCount = 0;
            WI_JobsDAL jdal = new WI_JobsDAL();
            List<WI_Jobs> joblist = jdal.GetAllJobs(JobShowStatus.Show, start, PageRows, out ttlCount);
            PagedList<WI_Jobs> pglist = joblist.ToPagedList(page, PageRows, ttlCount);
            return View(pglist);
        }

        public ActionResult SearchJobs(string search, string stype, int page = 1)
        {
            string decSearch = HttpUtility.UrlDecode(search.Trim());
            ViewBag.SearchKey = decSearch;
            page = page < 1 ? 1 : page;
            int start = (page - 1) * PageRows;
            int ttlCount;
            WI_JobsDAL jdal = new WI_JobsDAL();
            List<WI_Jobs> jobList = new List<WI_Jobs>();
            if (stype.ToLower() == "pos")
            {
                jobList = jdal.SearchByPosition(decSearch, start, PageRows, out ttlCount);
            }
            else
            {
                jobList = jdal.SearchByCompany(decSearch, start, PageRows, out ttlCount);
            }
            PagedList<WI_Jobs> pglist = jobList.ToPagedList(page, PageRows, ttlCount);
            ViewBag.SearchCount = ttlCount;
            return View(pglist);
        }

        [HttpPost]
        public ActionResult SearchJobs(string search, string stype)
        {
            string encSearch = HttpUtility.UrlEncode(search.Trim());
            return RedirectToAction("SearchJobs", "list", new { search = encSearch, stype = stype });
        }
    }
}
